package cn.wolfcode.trip.admin.web.controller;


import cn.wolfcode.trip.admin.web.util.UploadUtil;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    //根据id获取富文本的内容
    @RequestMapping("/getStrategyContentByStrategyId")
    @ResponseBody
    public Object getStrategyContentByStrategyId(Long strategyId){
       return strategyCatalogService.getStrategyContentByStrategyId(strategyId);
    }

    //新增或保存方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile pic, StrategyDetail strategyDetail) {
        JSONResult result = new JSONResult();

        if (pic != null && pic.getSize() > 0) {
            String imagePath = UploadUtil.upload(pic, UploadUtil.IMG_PATH + "upload");
            strategyDetail.setCoverUrl(imagePath);
        }

        try {
            strategyDetailService.saveOrUpdate(strategyDetail);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

    //攻略文章的列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQueryObject qo) {
        qo.setPageSize(10);
        model.addAttribute("result", strategyDetailService.getPageInfo(qo));
        model.addAttribute("strategies", strategyService.list());
        return "/strategyDetail/list";
    }


    //根据攻略文章的id获取大攻略的ID 用于回显数据
    @RequestMapping("/getStrategyIdByStrategyDetailId")
    @ResponseBody
    public Object getStrategyIdByStrategyDetailId() {
        /*select sc.strategy_id
        FROM strategydetail sd LEFT JOIN strategycatalog sc
        ON sd.catalog_id = sc.id
        WHERE sd.id  = 1*/
        return "";
    }


}
